<template>
  <div>
    <el-form :model="form" style="text-align: left">
      <el-row style="margin-bottom: 10px">
        <el-col :span="5" style="min-width: 50px">
          <div class="search-form-lable form-lable">数据源名称:</div>
        </el-col>
        <el-col :span="18" style="min-width: 90px">
          <el-input
            size="mini"
            v-model="form.name"
            autocomplete="off"
            style="float: left"
          ></el-input>
        </el-col>
      </el-row>

      <el-row style="margin-bottom: 10px">
        <el-input
          size="mini"
          v-model="form.id"
          autocomplete="off"
          v-show="false"
        ></el-input>
      </el-row>

      <el-row style="margin-bottom: 10px">
        <el-col :span="5" style="min-width: 50px">
          <div class="search-form-lable form-lable">数据库类型:</div>
        </el-col>
        <el-col :span="18" style="min-width: 90px">
          <el-select size="mini" v-model="form.type" placeholder="数据库类型">
            <el-option
              v-for="(item, index) in $global.DB_TYPE_ITEMS"
              :label="item.label"
              :key="index"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-col>
      </el-row>

      <el-row style="margin-bottom: 10px" v-if="form.type === 2">
        <el-col :span="5" style="min-width: 50px">
          <div class="search-form-lable form-lable">数据库连接:</div>
        </el-col>
        <el-col :span="18" style="min-width: 90px">
          <el-input
            size="mini"
            v-model="form.connectionStr"
            autocomplete="off"
            style="float: left"
          ></el-input>
        </el-col>
      </el-row>
      <el-row style="margin-bottom: 10px" v-if="form.type === 0">
        <el-col :span="5" style="min-width: 50px">
          <div class="search-form-lable form-lable">HOST:</div>
        </el-col>
        <el-col :span="18" style="min-width: 90px">
          <el-input
            size="mini"
            v-model="form.host"
            autocomplete="off"
            style="float: left"
          ></el-input>
        </el-col>
      </el-row>
      <el-row style="margin-bottom: 10px" v-if="form.type == 0">
        <el-col :span="5" style="min-width: 50px">
          <div class="search-form-lable form-lable">初始db:</div>
        </el-col>
        <el-col :span="18" style="min-width: 90px">
          <el-input
            size="mini"
            v-model="form.initDb"
            autocomplete="off"
            style="float: left"
          ></el-input>
        </el-col>
      </el-row>
      <el-row style="margin-bottom: 10px">
        <el-col :span="5" style="min-width: 50px">
          <div class="search-form-lable form-lable">用户名:</div>
        </el-col>
        <el-col :span="18" style="min-width: 90px">
          <el-input
            size="mini"
            v-model="form.username"
            autocomplete="off"
            style="float: left"
          ></el-input>
        </el-col>
      </el-row>

      <el-row style="margin-bottom: 10px">
        <el-col :span="5" style="min-width: 50px">
          <div class="search-form-lable form-lable">密码:</div>
        </el-col>
        <el-col :span="18" style="min-width: 90px">
          <el-input
            size="mini"
            v-model="form.password"
            autocomplete="off"
            style="float: left"
          ></el-input>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>

<script scoped>
const { ipcRenderer } = require('electron')
export default {
  name: 'connection-form',
  props: {
    paramerter: Object
  },
  components: {},
  computed: {},
  data() {
    return {
      loading: {},
      form: {
        name: '',
        type: '',
        host: '',
        initDb: '',
        connectionStr: '',
        username: '',
        password: ''
      }
    }
  },
  methods: {
    save() {
      this.$global.openFullScreen('保存中。。')
      this.$ipcRequest.request(
        {
          method: 'saveDbConnectionInfo',
          param: this.form
        },
        ({ data, message, code }) => {
          if (code === 0) {
            this.$message.success('保存成功!')
          } else {
            this.$message.error(message)
          }
          this.$global.closeFullScreen()
          this.$emit('useBrotherMethod', 'aside', 'getConnctionList')
          this.$emit('dialogClose')
        }
      )
    }
  },
  mounted() {},
  created() {
    if (this.paramerter.isAdd) {
      this.form = {
        name: '',
        type: '',
        connectionStr: '',
        username: '',
        password: ''
      }
    } else {
      this.form = {
        name: this.paramerter.name,
        type: this.paramerter.type,
        connectionStr: this.paramerter.connectionStr,
        username: this.paramerter.username,
        password: this.paramerter.password
      }
    }
  }
}
</script>

<style scoped>
</style>
